﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.DataAccess.Entities;
using CRM.Business.Services;
using CRM.Encryption;
using Telerik.Web.UI;
using System.IO;

namespace Web.Account
{
    public partial class AddNewRole : System.Web.UI.Page
    {
        static USER user;
        static ROLE r;
        static string prevPage = String.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;
            if (Request.UrlReferrer != null)
                prevPage = Request.UrlReferrer.ToString();
            user = (USER)Session["User"];
            if (user != null)
            {
                r = RoleService.GetRoleByID((long)user.ROLE_ID);
                if ((!(bool)user.IS_ADMIN) && (!(bool)r.IS_MANAGER))
                {
                    Response.Redirect("../Home.aspx");
                }
            }

        }
        private int addNewRole()
        {
            ROLE role = new ROLE();
            role.ROLE_NAME = txtRoleName.Text.Trim();
            if (btnToggleIsManger.Checked == true)
                role.IS_MANAGER = true;
            else
                role.IS_MANAGER = false;
            if (btnToggleActive.Checked == true)
                role.ACTIVE = true;
            else
                role.ACTIVE = false;
            role.DESCRIPTION = txtDescription.Text.Trim();
            return RoleService.AddNewRole(role);
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            int checkRole = RoleService.CheckExist(txtRoleName.Text.Trim());
            if (checkRole == 0)
            {
                int add = addNewRole();
                if (add == 1)
                {
                    RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessageAndRedirect('" + "Add new Role successful !" + "','" + "Roles.aspx" + "');");
                    //MessageBox.Show(this.Page, Page.GetType(), "Add new Role successful !", "Roles.aspx");
                }
                else
                    RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessage('" + "Can not add new Role !" + "');");
                    //MessageBox.Show(this.Page, Page.GetType(), "Cannot add new Role !", "");

            }
            else if (checkRole == 1)
                RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessage('" + "Role Name already exists !" + "');");
                //MessageBox.Show(this.Page, Page.GetType(), "Role Name already exists !", "");
        }
        protected void btn_Cancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(prevPage);
        }
        protected void RadAjaxManager1_AjaxRequest(object sender, Telerik.Web.UI.AjaxRequestEventArgs e)
        {
            
        }
    }
}